package com.luck.pf.security.role.logic;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.luck.org.entity.SysLog;
import com.luck.org.entity.SysMenu;
import com.luck.org.entity.SysRole;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.security.role.qry.RoleQry;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.Page;

/**
 * 说明:
 * Copyright: Copyright (c) 2010
 * @author zhangwr 2011-8-10 下午02:40:13
 * @version 1.0
 */
public class RoleLogic extends BaseLogic implements IRoleLogic {
	public List<SysRole> findRoles(RoleQry qry) throws Exception{
		Criteria criteria = super.baseDao.createCriteria(SysRole.class);
		if(qry.getRoleIds()!= null){
			criteria.add(Restrictions.in("id", qry.getRoleIds()));
		}
		//System.out.println(qry.getUserInfo().getDept().getDeptLevel());
		criteria.add(Restrictions.ge("roleLevel", qry.getUserInfo().getDept().getDeptLevel()));
		//criteria.createAlias("dept", "dept");
		//filterData(criteria, qry.getUserInfo(), "dept", null);
		criteria.addOrder(Order.asc("roleLevel"));
		//criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}
	public Page findRolePage(RoleQry qry) throws Exception{
		Criteria criteria = super.baseDao.createCriteria(SysRole.class);
		CriteriaUtil.addFullLike(criteria, "roleName", qry.getRoleName());
		CriteriaUtil.addEq(criteria, "roleLevel", qry.getRoleLevel());
		CriteriaUtil.addEq(criteria, "deptId", qry.getDeptId());
		criteria.createAlias("dept", "dept");
		filterData(criteria, qry.getUserInfo(), "dept", null);
		criteria.addOrder(Order.asc("roleLevel"));
		criteria.addOrder(Order.asc("id"));
		return super.baseDao.pagedQuery(criteria, qry.getPageNo(), qry.getPageSize());
	}
	public String saveRoleAuth(SysRole role, String menuIds) throws Exception{
		List<Long> ids = new ArrayList<Long>();
		String[] strs = menuIds.split(",");
		for(String str:strs){
			ids.add(Long.parseLong(str));
		}
		StringBuffer bf = new StringBuffer();
		Criteria criteria = super.baseDao.createCriteria(SysMenu.class);
		criteria.add(Restrictions.in("id", ids));
		List<SysMenu> menus = criteria.list();
		Set<SysMenu> set = new HashSet<SysMenu>(menus);
		role.setMenus(set);
		save(role);
		return bf.toString();
	}

	public String saveRole(SysRole role,UserBean userBean) throws Exception {
		StringBuffer bf = new StringBuffer();
		if(role.getId() == null){
			if(userBean.getDept().getDeptKind().equals("0")){
				if(userBean.getDept().getParentId()!=null)
					role.setDeptId(userBean.getDept().getParentId());
			}
		}
		save(role);
		return bf.toString();
	}
	public String deleteRole(SysRole role,UserBean userBean, SysLog log) throws Exception {
		StringBuffer bf = new StringBuffer();
		if(!userBean.getUser().getUserLevel().equals("1"))
			bf.append("您不是管理员<br/>");
		if(role.getUsers() != null && !role.getUsers().isEmpty())
			bf.append("此角色已被授权给用户<br/>");
		//if(role.getMenus() != null && !role.getMenus().isEmpty())
			//bf.append("此角色已被授权<br/>");
		if(StringUtils.isBlank(bf.toString()))
			super.delete(role);
		else
			throw new Exception(bf.toString());
		return bf.toString();
	}
}
